#include "SignalListeErzeuger.h"

SignalListenErzeuger::SignalListenErzeuger(string pfad)
{
	this->datei = pfad;
}

SignalListenErzeuger::SignalListenErzeuger(){}

vector<string> SignalListenErzeuger::getGatter() {
	fstream f;
	string aline;
    char line[256];
	string partName;

	int i = 0;

	f.open(SignalListenErzeuger::datei, ios::in);

	while (!f.eof())
    {
        f.getline(line, sizeof(line));
		aline = line;

		if( aline.length() != 0){
			if (aline.substr(0,1) == "g") {
			
				int pos = aline.find('(');
				partName = aline.substr(0,pos);
				gatter.push_back(partName);
			}
		}
	}
	return gatter;
}	

string SignalListenErzeuger::getDatei(){
	return this->datei;
}

vector<Signal> SignalListenErzeuger::readSignals(){
	fstream f;
	string aline;
    char line[256];
	string partName;

	vector<string> inp;
	vector<string> out;
	vector<string> inter;
	vector<string> glist;

	vector<Signal> res;
	Signal s;

	int i = 0;

	f.open(SignalListenErzeuger::datei, ios::in);

	while (!f.eof())
    {
		f.getline(line, sizeof(line));
		aline = line;

		if( aline.length() != 0){
			if(aline.substr(0,5) == "INPUT"){
				inp = this->split(aline.substr (6),',');
				inp.back().erase(4,1);
			}
			if(aline.substr(0,6) == "OUTPUT"){
				out = this->split(aline.substr (7),',');
				out.back().erase(4,1);
			}
			if(aline.substr(0,7) == "SIGNALS"){
				inter = this->split(aline.substr (8),',');
				inter.back().erase(4,1);
			}
			if(aline.substr(0,5) == "CLOCK"){
				string clk = this->split(aline.substr (6),',')[1];
				string hz = this->split(clk,' ')[2];
				string freq = this->split(clk,' ')[1];
				long bufferlong;
				if(hz == "MHz;"){
					std::stringstream ss(freq);
					ss >> bufferlong;
					this->frequenz = bufferlong;
				}else if(hz == "kHz;"){
					std::stringstream ss(freq);
					ss >> bufferlong;
					this->frequenz = bufferlong/1000;
				}else if(hz == "Hz;"){
					std::stringstream ss(freq);
					ss >> bufferlong;
					this->frequenz = bufferlong/1000000;
				}
			}
			if(aline.substr(0,1) == "g"){
				glist.push_back(aline);
			}
		}
	}

	Signal::signalTyp st;

	for(int i = 0; i < inp.size(); i++){
		s = Signal(inp.at(i));
		st = Signal::signalTyp::Eingang;
		s.setSignalTyp(st);
		res.push_back(s);
	}
	for(int i = 0; i < out.size(); i++){
		s = Signal(out.at(i));
		st = Signal::signalTyp::Ausgang;
		s.setSignalTyp(st);
		res.push_back(s);
	}
	for(int i = 0; i < inter.size(); i++){
		s = Signal(inter.at(i));
		st = Signal::signalTyp::Intern;
		s.setSignalTyp(st);
		res.push_back(s);
	}
	
	for(int i = 0; i < glist.size(); i++){
		string gatter = glist.at(i);
		int dot = gatter.find(':'); //=4
		int open = gatter.find('(');
		int close = gatter.find(')');
		string gattername = gatter.substr(0,dot);
		vector<string> signals = this->split(gatter.substr(open+1,close-open-1),',');
		
		string ausg = signals.back();
		signals.pop_back();

		for(int j = 0; j < signals.size(); j++){
			string signal = signals.at(j);
			for(int k = 0; k < res.size(); k++){
				Signal s = res.at(k);
				if(s.getName() == ausg){
					if(res.at(k).getQuelle() == gattername || res.at(k).getQuelle() == ""){  ///TODO Fehelr hier 
						res.at(k).setQuelle(gattername);
					}else{
						cout << "Fehler: Kurzschluss" << endl;
						system("pause");
						res.clear();
						Signal s = Signal("Kurzer");
						res.push_back(s);
						return res;
					}
				}
				if(s.getName() == signal){
					res.at(k).zielHinzufuegen(gattername);
				}
			}
		}
	}

	return res;
}

long SignalListenErzeuger::getFrequenz(){
	return this->frequenz;
}

std::vector<std::string> SignalListenErzeuger::split(std::string s, char delim) {
    std::vector<std::string> elems;
    std::stringstream ss(s);
    std::string item;
    while(std::getline(ss, item, delim)) {
        elems.push_back(item);
    }
	return elems;
}

void SignalListenErzeuger::dateiAusgabe(){
	cout << "Komplette Dateiausgabe der Schaltwerk:" << endl;
	
	std::fstream f;
	f.open(this->datei, ios::in);
	char line[300];
	string aline;

	while (!f.eof())
    {
		f.getline(line, sizeof(line));
		aline = line;
		cout << aline << endl;
	}

	system("pause");
}

SignalListenErzeuger::~SignalListenErzeuger(void)
{

}
